﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Accounting.DataAccess;

namespace Accounting.Controllers
{
    public class CategoryController : BaseController
    {
        private accountEntities db = new accountEntities();

        //
        // GET: /Category/

        public ViewResult Index()
        {
            return View(db.Categories.ToList());
        }

        //
        // GET: /Category/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Category/Create

        [HttpPost]
        public ActionResult Create(Category category)
        {
            if (ModelState.IsValid)
            {
                db.Categories.AddObject(category);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(category);
        }

        public ActionResult CreateSub(long categoryId)
        {
            Category category = db.Categories.Single(c => c.Id == categoryId);
            SubCategory sub = new SubCategory();
            sub.CategoryId = category.Id;
            sub.Category = category;
            return View(sub);
        }

        [HttpPost]
        public ActionResult CreateSub(SubCategory sub)
        {
            if (ModelState.IsValid)
            {
                db.SubCategories.AddObject(sub);
                db.SaveChanges();
                return RedirectToAction("Edit", new { id = sub.CategoryId });
            }

            return View(sub);
        }

        //
        // GET: /Category/Edit/5

        public ActionResult Edit(long id)
        {
            Category category = db.Categories.Single(c => c.Id == id);
            return View(category);
        }

        //
        // POST: /Category/Edit/5

        [HttpPost]
        public ActionResult Edit(Category category)
        {
            if (ModelState.IsValid)
            {
                db.Categories.Attach(category);
                db.ObjectStateManager.ChangeObjectState(category, EntityState.Modified);

                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(category);
        }

        public ActionResult EditSub(long id)
        {
            SubCategory sub = db.SubCategories.Single(s => s.Id == id);
            return View(sub);
        }

        [HttpPost]
        public ActionResult EditSub(SubCategory sub)
        {
            if (ModelState.IsValid)
            {
                db.SubCategories.Attach(sub);
                db.ObjectStateManager.ChangeObjectState(sub, EntityState.Modified);

                db.SaveChanges();
                return RedirectToAction("Edit", new { id = sub.CategoryId });
            }
            return View(sub);
        }

        //
        // GET: /Category/Delete/5

        public ActionResult Delete(long id)
        {
            Category category = db.Categories.Single(c => c.Id == id);
            return View(category);
        }

        //
        // POST: /Category/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(long id)
        {
            Category category = db.Categories.Single(c => c.Id == id);

            if (db.SubCategories.Any(s => s.CategoryId == id))
            {
                ModelState.AddModelError("", "Cannot delete category becaure it has sub category!");
                return View(category);
            }

            db.Categories.DeleteObject(category);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult DeleteSub(long id)
        {
            SubCategory sub = db.SubCategories.Single(s => s.Id == id);
            return View(sub);
        }

        [HttpPost, ActionName("DeleteSub")]
        public ActionResult DeleteSubConfirmed(long id)
        {
            SubCategory sub = db.SubCategories.Single(c => c.Id == id);
            db.SubCategories.DeleteObject(sub);
            db.SaveChanges();
            return RedirectToAction("Edit", new { id = sub.CategoryId });
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}